package nbn.android.myyoutubeclient.parse;

import nbn.android.myyoutubeclient.object.VideoObject;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.util.Log;

public class ParserVideo {
	/** --------------------- Movie Search TAG --------------------- */
	private static final String TAG_GROUP = "media:group";
	private static final String TAG_TITLE = "media:title";
	private static final String TAG_DESCRIPTION = "media:description";
	private static final String TAG_MEDIA_THUMB = "media:thumbnail";
	private static final String TAG_VIDEO_ID = "yt:videoid";
	private static final String TAG_DURATION = "yt:duration";
	private static final String TAG_TIME = "yt:uploaded";
	private static final String TAG_UPLOADER = "yt:uploaderId";
	private static final String TAG_VIEW = "yt:statistics";
	private static final String TAG_RATE = "gd:rating";
	private static final String TAG_LIKE = "yt:rating";

	/** ---------------------Movie Attribute tags--------------------- */
	private static final String ATR_THUMB_NAME = "yt:name";
	private static final String ATR_RATE_NUMRATERS = "numRaters";
	private static final String ATR_RATE_AVERAGE = "average";
	private static final String ATR_LIKE_NUMLIKE = "numLikes";
	private static final String ATR_LIKE_NUMDISLIKE = "numDislikes";
	private static final String ATR_VIEWCOUNT = "viewCount";
	private static final String ATR_DURATION = "seconds";
	/** ---------------------Movie Attribute tags--------------------- */
	private static final String VALUE_ATR_POSTER = "mqdefault";
	private static final String VALUE_ATR_DEFAULT = "default";
	
	public static VideoObject getResult(NodeList searchresult, int position) {
		VideoObject result = new VideoObject();
		try {
			Element e = (Element) searchresult.item(position);
			String rate = Parser.getAttributeValue(e, TAG_RATE, ATR_RATE_AVERAGE);
			String viewed = Parser.getAttributeValue(e, TAG_VIEW, ATR_VIEWCOUNT);
			String like = Parser.getAttributeValue(e, TAG_LIKE, ATR_LIKE_NUMLIKE);
			String dislike = Parser.getAttributeValue(e, TAG_LIKE, ATR_LIKE_NUMDISLIKE);
			NodeList list=e.getElementsByTagName(TAG_GROUP);
			Element e1=(Element)list.item(0);
			String description = Parser.getValue(e1, TAG_DESCRIPTION);
			String title = Parser.getValue(e1, TAG_TITLE);
			String uploaded = Parser.getValue(e1, TAG_TIME);
			String uploaderid = Parser.getValue(e1, TAG_UPLOADER);
			String video_id = Parser.getValue(e1, TAG_VIDEO_ID);
			String image_url=Parser.getImageUrlAttributeValue(TAG_MEDIA_THUMB,ATR_THUMB_NAME,VALUE_ATR_POSTER, e1);
			if(image_url==null){
				image_url = Parser.getImageUrlAttributeValue(TAG_MEDIA_THUMB, ATR_THUMB_NAME,VALUE_ATR_DEFAULT, e1);
			}
			String duration=Parser.getAttributeValue(e1, TAG_DURATION, ATR_DURATION);			
			
			result.setrate(rate);
			result.setview(viewed);
			result.setlike(like);
			result.setdislike(dislike);
			result.settitle(title);
			result.settime(uploaded);
			result.setVideo_id(video_id);
			result.setDuration(duration);
			result.setthumbnail(image_url);
			result.setdescription(description);
			result.setuploaderId(uploaderid);
			result.showinfo();
//			Log.i("duration","___________________\n");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}